package com.phiture.erp.basic.core.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.phiture.erp.basic.common.utils.BasicCodeGenerator;
import com.phiture.erp.basic.core.pojo.dto.ErpSupplierPageReqDTO;
import com.phiture.erp.basic.core.pojo.dto.ErpSupplierSaveReqDTO;
import com.phiture.erp.basic.core.service.ErpSupplierService;
import com.phiture.erp.basic.dal.entity.ErpSupplierDO;
import com.phiture.erp.basic.dal.mapper.ErpSupplierMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static com.phiture.erp.common.constants.ErrorCodeConstants.SUPPLIER_NOT_ENABLE;
import static com.phiture.erp.common.constants.ErrorCodeConstants.SUPPLIER_NOT_EXISTS;

/**
 * @author YH
 * @date 2025/3/28
 */

@Service
@RequiredArgsConstructor
public class ErpSupplierServiceImpl implements ErpSupplierService {
    private final ErpSupplierMapper supplierMapper;

    @Override
    public Long createSupplier(ErpSupplierSaveReqDTO createReqDTO) {
        ErpSupplierDO supplier = BeanUtils.toBean(createReqDTO, ErpSupplierDO.class);
        supplier.setSupplierCode(BasicCodeGenerator.generateSupplierCode());
        supplierMapper.insert(supplier);
        return supplier.getId();
    }

    @Override
    public void updateSupplier(ErpSupplierSaveReqDTO updateReqDTO) {
        // 校验存在
        validateSupplierExists(updateReqDTO.getId());
        // 更新
        ErpSupplierDO supplierDO = BeanUtils.toBean(updateReqDTO, ErpSupplierDO.class);
        supplierMapper.updateById(supplierDO);
    }

    @Override
    public void deleteSupplier(Long id) {
        // 校验存在
        validateSupplierExists(id);
        // 删除
        supplierMapper.deleteById(id);
    }

    @Override
    public ErpSupplierDO getSupplier(Long id) {
        return supplierMapper.selectById(id);
    }

    @Override
    public ErpSupplierDO validateSupplier(Long id) {
        ErpSupplierDO supplier = supplierMapper.selectById(id);
        if (supplier == null) {
            throw exception(SUPPLIER_NOT_EXISTS);
        }
        if (CommonStatusEnum.isDisable(supplier.getStatus())) {
            throw exception(SUPPLIER_NOT_ENABLE, supplier.getName());
        }
        return supplier;
    }

    @Override
    public List<ErpSupplierDO> getSupplierList(Collection<Long> ids) {
        if(CollUtil.isEmpty(ids)){
            return Collections.emptyList();
        }
        return supplierMapper.selectBatchIds(ids);
    }

    @Override
    public Map<Long, ErpSupplierDO> getSupplierMap(Collection<Long> ids) {
        return convertMap(getSupplierList(ids), ErpSupplierDO::getId);
    }

    @Override
    public PageResult<ErpSupplierDO> getSupplierPage(ErpSupplierPageReqDTO pageReqDTO) {
        return supplierMapper.selectPage(pageReqDTO, new LambdaQueryWrapperX<ErpSupplierDO>()
                .likeIfPresent(ErpSupplierDO::getName, pageReqDTO.getName())
                .likeIfPresent(ErpSupplierDO::getMobile, pageReqDTO.getMobile())
                .likeIfPresent(ErpSupplierDO::getTelephone, pageReqDTO.getTelephone())
                .eqIfPresent(ErpSupplierDO::getSupplierCode, pageReqDTO.getSupplierCode())
                .orderByDesc(ErpSupplierDO::getId));
    }

    @Override
    public List<ErpSupplierDO> getSupplierListByStatus(Integer status) {
        return supplierMapper.selectList(ErpSupplierDO::getStatus, status);
    }

    @Override
    public Map<Long, String> getSupplierMap() {
        List<ErpSupplierDO> list = supplierMapper.selectList(ErpSupplierDO::getDeleted, Boolean.FALSE);
        return CollUtil.isEmpty(list) ? Collections.emptyMap() : list.stream().collect(Collectors.toMap(ErpSupplierDO::getId, ErpSupplierDO::getName));
    }

    private void validateSupplierExists(Long id) {
        if (supplierMapper.selectById(id) == null) {
            throw exception(SUPPLIER_NOT_EXISTS);
        }
    }
}
